package cn.tedu.onlinestore.cart.controller;

import cn.tedu.onlinestore.base.response.JsonResult;
import cn.tedu.onlinestore.cart.pojo.dto.CartParam;
import cn.tedu.onlinestore.cart.pojo.entity.Cart;
import cn.tedu.onlinestore.cart.pojo.vo.CartVO;
import cn.tedu.onlinestore.cart.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/v1/cart/")
public class CartController {
    @Autowired
    CartService cartService;

    @RequestMapping("add")
    public JsonResult addCart(CartParam cartParam) {
        log.debug("添加购物车：{}",cartParam);
        cartService.addCart(cartParam);
        return JsonResult.ok();
    }

    @GetMapping("list/{id}")
    public JsonResult cartList(@PathVariable Long id) {
        log.debug("查询购物车列表：{}",id);
        List<CartVO> cartList = cartService.cartList(id);
        return JsonResult.ok(cartList);
    }

    @PostMapping("update")
    public JsonResult updateCart(@RequestBody ArrayList<CartVO> cartList) {
        log.debug("修改购物车：{}",cartList);
        cartService.update(cartList);
        return JsonResult.ok();
    }
    @PostMapping("delete/{id}")
    public JsonResult deleteCart(@PathVariable Long id) {
        log.debug("删除购物车：{}",id);
        cartService.deleteCart(id);
        return JsonResult.ok();
    }

}
